<?php

namespace app\admin\service\user_profile\userArchive\psychologicalCounseling;

use app\admin\service\user_profile\userArchive\evaluationArchive\EvaluationArchiveService;
use app\admin\service\UserService;
use app\common\Message;
use app\model\ConsultOfflineModel;
use app\model\ConsultOnlineModel;
use app\model\ConsultQuestionModel;
use app\model\UserModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Service;

class PsychologicalCounselingService extends Service
{
    use Message;

    /**
     * @param $data
     * @return true
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getOverview($data): bool
    {
        //用户基本信息
        $getUserInfo = new EvaluationArchiveService($this->app);
        $info = $getUserInfo->getUserInfo($data);
        //预约咨询数据统计
        $ConsultOnlineModel = new ConsultOnlineModel();
        $ConsultOfflineModel = new ConsultOfflineModel();
        $ConsultQuestionModel = new ConsultQuestionModel();
        if(check_array_key('isAbandon',$data)){
            //废弃档案
            $tempWhere = [
                ['uid','=',$data['user_id']],
                ['is_repeal','=',1],
                ['is_end','=',1],
            ];
        }else{
            //正常档案
            $tempWhere = [
                ['uid','=',$data['user_id']],
                ['is_repeal','=',0],
                ['is_end','=',1],
            ];
        }
        $offline_uids = $ConsultOfflineModel->where($tempWhere)->field('id,time')->select();
        $online_uids = $ConsultOnlineModel->where($tempWhere)->field('id,time')->select();
        $temp['count'] = count($offline_uids)+count($online_uids);
        if($temp['count']>0){
            $tempArray = array_merge($offline_uids->toArray(),$online_uids->toArray());
            $timeArray = array_column($tempArray, 'time');
            $ret['min_time'] = min($timeArray);
            $ret['to_date'] = ceil((time() - $ret['min_time']) / (24 * 60 * 60));
            $question = $ConsultQuestionModel->where('unit_id','=',$data['unit_id'])->field('id,name')->select()->toArray();
            $lists = [];
            $time = 0;
            foreach ($question as &$item) {
                if(check_array_key('isAbandon',$data)){
                    //废弃档案
                    $tempWheres = [
                        ['uid','=',$data['user_id']],
                        ['question_id','=',$item['id']],
                        ['is_repeal','=',1],
                        ['is_end','=',1],
                    ];
                }else{
                    //正常档案
                    $tempWheres = [
                        ['uid','=',$data['user_id']],
                        ['question_id','=',$item['id']],
                        ['is_repeal','=',0],
                        ['is_end','=',1],
                    ];
                }
                $offline_uids = $ConsultOfflineModel->where($tempWheres)->count();
                $offline_uids_time = $ConsultOfflineModel->where($tempWheres)->max('time');
                if($offline_uids_time>$time){
                    $time = $offline_uids_time;
                }
                $online_uids = $ConsultOnlineModel->where($tempWheres)->count();
                $online_uids_time = $ConsultOnlineModel->where($tempWheres)->max('time');
                if($online_uids_time>$time){
                    $time = $online_uids_time;
                }
                $item['num'] = $offline_uids+$online_uids;
                $item['ratio'] = $temp['count']?round($item['num']/$temp['count']*100,1) . '%':'0%';
                $lists[] = $item;
            }
            foreach ($lists as $key => $row) {
                $field[$key]  = $row['num'];
            }
            if (count($lists)) array_multisort($field, SORT_DESC, $lists); // 假设按’field’字段升序排序

            $info['psychological'] = paginateArray($lists,$data['page'],$data['pageSize']);
            $info['psychological']['min_time'] = $ret['min_time'];
            $info['psychological']['to_date'] = $ret['to_date'];
            $info['psychological']['total'] = $temp['count'];
            $info['psychological']['max_time'] = $time;
        }else{
            $info = [];
        }
        $this->result = $info;
        $this->message = '操作成功！';
        return true;
    }

    /**
     * @param $data
     * @return true
     */
    public function getOnlineRecode($data): bool
    {
        if(check_array_key('isAbandon',$data)){
            //废弃档案
            $where = [
                ['uid','=',$data['user_id']],
                ['is_end','=',1],
                ['is_repeal','=',1],
            ];
        }else{
            //正常档案
            $where = [
                ['uid','=',$data['user_id']],
                ['is_end','=',1],
                ['is_repeal','=',0],
            ];
        }
        $return = ConsultOnlineModel::query()->where($where)
            ->with(['questionName','consult'])
            ->field('id,consult_id,question_id,is_end,end_time')
            ->order('end_time desc')
            ->select();
        $ret = [];
        if(count($return)>0){
            $sort_by_month = new EvaluationArchiveService($this->app);
            $ret = $sort_by_month->sort_by_month($return->toArray(),'end_time',$data);
        }
        $this->result = $ret;
        $this->message = '操作成功！';
        return true;
    }

    /**
     * @param $data
     * @return true
     * @throws DbException
     */
    public function getOfflineRecode($data): bool
    {
        if(check_array_key('isAbandon',$data)){
            //废弃档案
            $where = [
                ['uid','=',$data['user_id']],
                ['is_end','=',1],
                ['is_repeal','=',1],
            ];
        }else{
            //正常档案
            $where = [
                ['uid','=',$data['user_id']],
                ['is_end','=',1],
                ['is_repeal','=',0],
            ];
        }
        $return = ConsultOfflineModel::query()->where($where)
            ->with(['questionName','consult','ConsultTime'])
            ->field('id,consult_id,question_id,is_end,end_time,type,situation,time_id')
            ->order('end_time desc')
            ->select();
        $ret = [];
        if(count($return)>0){
            $sort_by_month = new EvaluationArchiveService($this->app);
            $ret = $sort_by_month->sort_by_month($return->toArray(),'end_time',$data);
        }
        $this->result = $ret;
        $this->message = '操作成功！';
        return true;
    }
}